import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * @Author: 渔小鲤
 * @DateTime: 2025/7/12 18:04
 **/
public class test {
    public static void main(String[] args) throws IOException {
        Path rootPath = Paths.get("C:\\JAVA\\code\\HudsonMQ");

        if (!Files.exists(rootPath)) {
            System.err.println("路径不存在: " + rootPath.toAbsolutePath());
            return;
        }

        long totalLines = 0;

        try (Stream<Path> walk = Files.walk(rootPath)) {
            totalLines = walk
                    .filter(path -> path.toString().endsWith(".java"))
                    .peek(path -> {
                        try {
                            long lines = Files.lines(path).count();
                            System.out.println("文件: " + path.getFileName() + " - 行数: " + lines);
                        } catch (IOException e) {
                            System.err.println("读取文件失败: " + path);
                        }
                    })
                    .mapToLong(path -> {
                        try {
                            return Files.lines(path).count();
                        } catch (IOException e) {
                            return 0;
                        }
                    })
                    .sum();

        } catch (IOException e) {
            System.err.println("遍历目录时发生错误");
            e.printStackTrace();
        }

        System.out.println("总计代码行数: " + totalLines);
    }
}
